//
//  MyStockForceTraceController.m
//  Stock
//
//  Created by blacksun on 11-7-1.
//  Copyright 2011年 __MyCompanyName__. All rights reserved.
//

#import "MyStockForceTraceController.h"
#import "BarChartViewController.h"


@implementation MyStockForceTraceController

@synthesize parentController;
@synthesize myStockService;
@synthesize downloadImageService;
@synthesize forceTrace;
@synthesize label1;
@synthesize label2;
@synthesize label3;
@synthesize label4;
@synthesize label5;
@synthesize label6;
@synthesize label7;
@synthesize label8;
@synthesize label9;
@synthesize imageView;
@synthesize selectedImage;
@synthesize myStock;
@synthesize updateTime;
@synthesize stockImageTimes;
@synthesize pubDateLabel;
@synthesize childViewControllers;

#pragma mark -Service Delegate
-(void)requestFinishedWithResult:(id)aResult andUserinfo:(NSDictionary *)aUserinfo{
    NSString *action = [aUserinfo objectForKey:kASIHttpRequestUserinfoKey];
    if ([action isEqualToString:kMyStockServiceQueryForceTrace]) {
        self.forceTrace = aResult;
        myStock.ft = updateTime;
        [self reloadViews];
    }
}
-(void)requestFinishedwithError:(NSError *)aError andUserinfo:(NSDictionary *)aUserinfo{
    
}
-(void)reloadViews{
    UIColor *color3 = [UIColor redColor];
    UIColor *color10 = [UIColor redColor];
    UIColor *color60 = [UIColor redColor];
    if ([forceTrace.f3 hasPrefix:@"-"]) {
        color3 = [UIColor greenColor];
    }
    if ([forceTrace.f10 hasPrefix:@"-"]) {
        color10 = [UIColor greenColor];
    }
    if ([forceTrace.f60 hasPrefix:@"-"]) {
        color60 = [UIColor greenColor];
    }
    label1.textColor = color3;
    label2.textColor = color3;
    //label3.textColor = color3;
    label4.textColor = color10;
    label5.textColor = color10;
    //label6.textColor = color10;
    label7.textColor = color60;
    label8.textColor = color60;
    //label9.textColor = color60;
    self.label1.text = forceTrace.f3;
    self.label4.text = forceTrace.f10;
    self.label7.text = forceTrace.f60;
    self.label3.text = forceTrace.turnover3;
    self.label6.text = forceTrace.turnover10;
    self.label9.text = forceTrace.turnover60;
    self.label2.text = forceTrace.fr3;
    self.label5.text = forceTrace.fr10;
    self.label8.text = forceTrace.fr60;
}

-(IBAction)buttonClickedAction:(id)sender{
    UIButton *button = (UIButton *)sender;
    if (currentButtonTag == button.tag) {
        return;
    }
    BarChartViewController *clickedController = [childViewControllers objectAtIndex:button.tag+1];
    BarChartViewController *lastClickedController = [childViewControllers objectAtIndex:currentButtonTag+1];
    CGRect orginFrame = lastClickedController.view.frame;
    CGRect moveToFrame;
    if (button.tag > currentButtonTag) {//左移
        clickedController.view.frame = CGRectMake(320.0, orginFrame.origin.y, orginFrame.size.width, orginFrame.size.height);
        moveToFrame = CGRectMake(-320.0, orginFrame.origin.y, orginFrame.size.width, orginFrame.size.height);
    }else{
        clickedController.view.frame = CGRectMake(-320.0, orginFrame.origin.y, orginFrame.size.width, orginFrame.size.height);
        moveToFrame = CGRectMake(320.0, orginFrame.origin.y, orginFrame.size.width, orginFrame.size.height);
    }
    if (!clickedController.view.superview) {
        [self.view addSubview:clickedController.view];
    }
    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationCurve:UIViewAnimationCurveEaseInOut];
    [UIView setAnimationDuration:0.3];
    lastClickedController.view.frame = moveToFrame;
    clickedController.view.frame = CGRectMake(0.0, orginFrame.origin.y, orginFrame.size.width, orginFrame.size.height);
    [UIView commitAnimations];
    
    
    //[self setUpImageViewWithImageType:type];
    currentButtonTag = button.tag;
    CGRect frame = button.frame;
    CGRect oldFrame = selectedImage.frame;
    self.selectedImage.frame = CGRectMake(frame.origin.x, oldFrame.origin.y, oldFrame.size.width, oldFrame.size.height);
}

- (void)dealloc
{
    [self.myStockService cancleAllRequest];
    self.myStockService = nil;
    [self.downloadImageService cancleAllRequest];
    self.downloadImageService = nil;
    [forceTrace save];
    self.forceTrace = nil;
    self.label1 = nil;
    self.label2 = nil;
    self.label3 = nil;
    self.label4 = nil;
    self.label5 = nil;
    self.label6 = nil;
    self.label7 = nil;
    self.label8 = nil;
    self.label9 = nil;
    self.imageView = nil;
    self.selectedImage = nil;
    self.updateTime = nil;
    self.stockImageTimes = nil;
    self.pubDateLabel = nil;
    self.childViewControllers = nil;
        self.myStock = nil;
    [super dealloc];
}

- (void)didReceiveMemoryWarning
{
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
}

#pragma mark - View lifecycle

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.childViewControllers = [NSMutableArray array];
    [self performSelector:@selector(updateParent) withObject:nil afterDelay:0.1];
    self.myStockService = [[[MyStockService alloc] initWithDelegate:self] autorelease];
    self.downloadImageService = [[[DownloadImageService alloc] initWithDelegate:self] autorelease];
    self.stockImageTimes = [NSMutableDictionary dictionary];
    
    currentButtonTag = -1;
    
    [self setupSelf];
    
    NSDate *date = [[NSDate alloc] initWithTimeIntervalSince1970:[self.updateTime longLongValue]/1000];
    NSDateFormatter *dateFormatter = [[NSDateFormatter alloc] init];
    [dateFormatter setDateFormat:@"yyyy-MM-dd HH:mm:ss"];
    self.pubDateLabel.text = [dateFormatter stringFromDate:date];
    [date release];
    [dateFormatter release];
    
    [self reloadViews];
    
}
-(void)setupSelf{
    self.forceTrace = [MyStockForceTrace findByStockId:myStock.uuid];
    MyStock *remoteMyStock = [[StockContext shareInstance] findRemoteStockWithStockId:myStock.uuid];
    self.updateTime = remoteMyStock.ft;
    if (!forceTrace || ![updateTime isEqualToString:myStock.ft]) {
        [myStockService queryStockForceTraceWithStockId:myStock.uuid];
    }
    
    chartFrame = CGRectMake(0.0, 257, 320.0, 120.0);
    
    BarChartViewController *dayController = [[BarChartViewController alloc] initWithNibName:@"BarChartViewController" bundle:nil];
    dayController.stockId = myStock.uuid;
    dayController.chartType = @"d";
    dayController.serviceType = @"fftc";
    dayController.view.frame = chartFrame;
    BarChartViewController *weekController = [[BarChartViewController alloc] initWithNibName:@"BarChartViewController" bundle:nil];
    weekController.stockId = myStock.uuid;
    weekController.chartType = @"w";
    weekController.serviceType = @"fftc";
    
    BarChartViewController *monthController = [[BarChartViewController alloc] initWithNibName:@"BarChartViewController" bundle:nil];
    monthController.stockId = myStock.uuid;
    monthController.chartType = @"m";
    monthController.serviceType = @"fftc";
    
    
    [self.view addSubview:dayController.view];
    
    [self.childViewControllers addObject:dayController];
    [self.childViewControllers addObject:weekController];
    [self.childViewControllers addObject:monthController];
    [dayController release];
    [weekController release];
    [monthController release];
    
    
}
-(void)updateParent{
    [parentController childViewDidUpdated:self.view.frame.size.height];
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    [myStockService cancleAllRequest];
    self.myStockService = nil;
    [downloadImageService cancleAllRequest];
    self.downloadImageService = nil;
    self.forceTrace = nil;
    self.label1 = nil;
    self.label2 = nil;
    self.label3 = nil;
    self.label4 = nil;
    self.label5 = nil;
    self.label6 = nil;
    self.label7 = nil;
    self.label8 = nil;
    self.label9 = nil;
    self.imageView = nil;
    self.selectedImage = nil;
    self.stockImageTimes = nil;
    self.pubDateLabel = nil;
    self.childViewControllers = nil;
    self.myStock = nil;
    
}

@end
